﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterMove
{
    private Vector3 moveDir;
    private CharacterController controller;
    private CharacterDirectionBase direction;

    private float jumpCount = 0;
    private PlayerMoveProperties moveProperties;



    public void InputMove()
    {
        moveDir = Vector3.zero;

        if (Input.GetKey(KeyCode.W))
        {
            moveDir += direction.Forward;
        }
        if(Input.GetKey(KeyCode.S))
        {
            moveDir -= direction.Forward;
        }
        if(Input.GetKey(KeyCode.A))
        {
            moveDir -= direction.Right;
        }
        if(Input.GetKey(KeyCode.D))
        {
            moveDir += direction.Right;
        }

        moveDir = moveDir.normalized;
        moveDir *= moveProperties.MoveSpeed;

        SetJumpedMoveDir();
        controller.Move(moveDir * Time.deltaTime);
    }



    private void SetJumpedMoveDir()
    {
        var enableJump = GetJumpStatus();

        if(enableJump)
        {
            jumpCount = moveProperties.JumpForce;
        }
        jumpCount -= moveProperties.Gravity*Time.deltaTime;
        moveDir.y = jumpCount;
    }

    private bool GetJumpStatus()
    {
        var hitJumpKey = Input.GetKeyDown(KeyCode.Space);
        var isGrounded = controller.isGrounded;
        var activeJump = moveProperties.ActiveJump;

        var enableJump = hitJumpKey&&isGrounded&&activeJump;
    
        return enableJump;
    }
        


    private void SetCharacterDirection(CharacterController controller)
    {
        if(moveProperties.EnableLocalDirection)
            direction = new CharacterDirectionBase(controller);
        else
            direction = new CharacterDirectionBase(null);
    }

    private CharacterMove() { }
    public CharacterMove(CharacterController controller,PlayerMoveProperties properties)
    {
        this.controller = controller;
        this.moveProperties = properties;

        SetCharacterDirection(controller);
    }
}
